package cn.yanhu.kuwanapp.my.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import cn.woochen.comlib.BaseActivity
import cn.woochen.comlib.util.CacheUtil
import cn.woochen.comlib.util.OnSingleClickListener
import cn.woochen.comlib.util.PackageUtil
import cn.woochen.comlib.util.ext.setOnSingleClickListener
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.common.activity.CommonWebviewActivity
import cn.yanhu.kuwanapp.databinding.ActivitySettingBinding
import cn.yanhu.kuwanapp.home.MainViewModel
import cn.yanhu.kuwanapp.utils.Config
import cn.yanhu.kuwanapp.utils.SimpleDialogUtil
import cn.yanhu.kuwanapp.utils.ext.showToast
import cn.yanhu.kuwanapp.utils.helper.UserManageHelper

/**
 * 类描述：设置
 * @author：woochen
 * create at：2021/8/7 12:03 AM
*/

class SettingActivity : BaseActivity<ActivitySettingBinding,MainViewModel>(),
    OnSingleClickListener {

    override fun setContentView(): Int = R.layout.activity_setting

    override fun initData() {
        setStatusBarStyle(false)
        mBinding.tvVersionName.text = "v${PackageUtil.getCurrentVersionName(mContext)}"
    }

    override fun initListener() {
        mBinding.tvUserProtocol.setOnSingleClickListener(this)
        mBinding.tvPrivacyProtocol.setOnSingleClickListener(this)
        mBinding.rlClearCache.setOnSingleClickListener(this)
        mBinding.tvLogout.setOnSingleClickListener(this)
    }

    companion object {

        fun start(context: Context) {
            val intent = Intent(context, SettingActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onSingleClick(v: View?) {
        when (v) {
            mBinding.tvUserProtocol -> {//用户协议
                CommonWebviewActivity.start(mContext, Config.USER_AGREEMENT,true)
            }
            mBinding.tvPrivacyProtocol -> {//隐私协议
                CommonWebviewActivity.start(mContext, Config.USER_PRIVATE_AGREEMENT,true)
            }
            mBinding.rlClearCache -> {//清除缓存
                if (CacheUtil.getTotalCacheSize(mContext) == "0K") {
                    return
                }
                CacheUtil.clearAllCache(mContext)
                showToast("缓存已清理")
                mBinding.tvCache.text = "0K"
            }
            mBinding.tvLogout -> {//退出登录
                val create = SimpleDialogUtil.create(mContext, layoutRes = R.layout.dialog_exit_account)
                create?.apply {
                    val tvCancel = getView<TextView>(R.id.tv_cancel)
                    val tvQuit = getView<TextView>(R.id.tv_quit)
                    tvCancel?.setOnClickListener {
                        dismiss()
                    }
                    tvQuit?.setOnClickListener {
                        UserManageHelper.reLogin()
                        dismiss()
                    }

                }?.show()
            }

        }
    }
}